package com.cskaoyan.mall.dao;

import com.cskaoyan.mall.model.UpdateUserPwd;
import com.cskaoyan.mall.model.admin.AdminLoginBO;
import com.cskaoyan.mall.model.user.User;
import com.cskaoyan.mall.model.user.UserForUpdate;
import com.cskaoyan.mall.util.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

import static com.cskaoyan.mall.util.UpdateOrAddAdminUtils.judgePassword;

/**
 * @author: jyf2000jyf
 */
public class UserDaoImpl implements UserDao {
    @Override
    public List<User> getAllUser() {
        List<User> userList = null;
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            userList = queryRunner.query("select * from user", new BeanListHandler<>(User.class));
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            return userList;
        }
    }

    @Override
    public boolean deleteUser(int id) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            queryRunner.update("delete from user where id = ?", id);
        } catch (SQLException e) {
            return false;
        }
        return true;
    }

    @Override
    public List<User> getSearchUser(String word) {
        List<User> userList = null;
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            userList = queryRunner.query("select * from user where nickname like ?", new BeanListHandler<>(User.class), "%" + word + "%");
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            return userList;
        }
    }

    @Override
    public int login(AdminLoginBO adminLoginBO) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            Long query = queryRunner.query("select count(*) from user where username = ? and password = ?", new ScalarHandler<>(), adminLoginBO.getEmail(), adminLoginBO.getPwd());
            if (query == 1) {
                return 200;
            } else {
                return 404;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 500;
    }

    @Override
    public boolean addUser(User user) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            queryRunner.update("insert into user values(null,?,?,?,?,?,?)", user.getUsername(), user.getNickname(), user.getPassword(), user.getRecipient(), user.getAddress(), user.getPhone());
        } catch (SQLException e) {
            return false;
        }
        return true;
    }

    @Override
    public Integer getUserIdByUsername(String username) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        Integer userId = null;
        try {
            User user = queryRunner.query("select * from user where username = ?", new BeanHandler<>(User.class), username);
            userId = user.getId();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userId;
    }

    @Override
    public String getUserNickname(AdminLoginBO adminLoginBO) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        String nickname = null;
        try {
            User user = queryRunner.query("select * from user where username = ?", new BeanHandler<>(User.class), adminLoginBO.getEmail());
            nickname = user.getNickname();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return nickname;
    }

    @Override
    public User getUserByUsername(String token) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        User user = null;
        try {
            user = queryRunner.query("select * from user where username = ?", new BeanHandler<>(User.class), token);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public UserForUpdate getUserByUserName(String token) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        UserForUpdate user = null;
        try {
            user = queryRunner.query("select id,username as email,nickname,recipient,address,phone from user where username = ?", new BeanHandler<>(UserForUpdate.class), token);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public boolean updateUserData(UserForUpdate userForUpdate) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            queryRunner.update("update user set nickname = ?,recipient = ?,address = ?,phone = ? where id = ?",userForUpdate.getNickname(),
                    userForUpdate.getRecipient(),userForUpdate.getAddress(),userForUpdate.getPhone(),userForUpdate.getId());
        } catch (SQLException e) {
            return false;
        }
        return true;
    }

    @Override
    public String updatePwd(UpdateUserPwd pwd) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            Long query = queryRunner.query("select count(*) from user where id = ? and password = ?", new ScalarHandler<>(), pwd.getId(), pwd.getOldPwd());
            if (query.intValue() == 0) {
                return "原密码错误";
            }
            if (!judgePassword(pwd.getNewPwd())) {
                return "密码正则验证未通过";
            }
            queryRunner.update("update user set password = ? where id = ? and password = ?", pwd.getNewPwd(), pwd.getId(), pwd.getOldPwd());
        } catch (SQLException e) {
            return "sql异常";
        }
        return pwd.getNewPwd();
    }
}
